package common.spring.base;

import common.spring.jdbc.MySQLHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;


@Repository
public class BaseDao {

    @Autowired
    private MySQLHelper jdbcHelper;

    /**
     * 数据权限部门增删改时更新所有deptIds
     * 注:只能是内部调用
     * @param tableName
     * @param deptId1
     * @param deptId2
     * @param type
     * @return
     */
    public int editDeptIdsAll(String tableName, String deptId1, String deptId2, String type){
        StringBuilder sql = new StringBuilder();
        int result = 0;
        if("add".equals(type)){
            sql.append(" update ");
            sql.append(tableName);
            sql.append(" set deptIds=CONCAT(deptIds,?) ");
            result = jdbcHelper.update(sql.toString(),new Object[]{deptId1});
        }
        if("update".equals(type)){
            sql.append(" update ");
            sql.append(tableName);
            sql.append(" set deptIds=replace(deptIds,?,?) ");
            result =  jdbcHelper.update(sql.toString(),new Object[]{deptId1,deptId2});
        }
        return result;
    }

}
